import router,

for zdj_cnt=1:1
clc
clear temp_dst_routers;
clear mesh_routers;
clear ordered_distances;
clear distances_order;
clear src_dst_distances;

flag = 0;
mesh_x_size = 7;
mesh_y_size = 7;
number_of_dst = 24;
number_of_crash_routers = 0;
number_of_crash_channels = 0;
src_router = [4,4];
crash_routers = zeros(number_of_crash_routers,2);
temp_dst_routers = zeros(number_of_dst,2);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
cnt = 1;
while (cnt < number_of_crash_routers + 1)
    rand_router(1,1) = ceil(mesh_x_size*rand(1));
    rand_router(1,2) = ceil(mesh_y_size*rand(1));
    rand_false = 0;
    for i=1:cnt
        if((sum(crash_routers(i,:) ==  rand_router) == 2) || ((rand_router(1,1) == src_router(1)) && (rand_router(1,2) == src_router(2))))
            rand_false = 1;
        end
    end
    if (~rand_false)
        crash_routers(cnt,1) = rand_router(1,1);
        crash_routers(cnt,2) = rand_router(1,2);
        cnt = cnt + 1;
    end
end
%crash_routers = [3,4;2,6;3,3;3,6;4,3;6,5;3,5;6,3;6,4;4,2;];
crash_routers = [5,4;5,6;3,2;6,5;3,1;4,2;4,3;2,5;4,5;1,5;];

dst_cnt = 1;
while (dst_cnt < number_of_dst + 1)
    rand_router(1,1) = ceil(mesh_x_size*rand(1));
    rand_router(1,2) = ceil(mesh_y_size*rand(1));
    rand_false = 0;
    for i=1:dst_cnt
        if((sum(temp_dst_routers(i,:) ==  rand_router) == 2) || ((rand_router(1,1) == src_router(1)) && (rand_router(1,2) == src_router(2))))
            rand_false = 1;
        end
    end
    for i=1:number_of_crash_routers
        if((sum(crash_routers(i,:) ==  rand_router) == 2))
            rand_false = 1;
        end
    end
    if (~rand_false)
        temp_dst_routers(dst_cnt,1) = rand_router(1,1);
        temp_dst_routers(dst_cnt,2) = rand_router(1,2);
        dst_cnt = dst_cnt + 1;
    end
end
%temp_dst_routers = [1,2;1,3;1,6;4,1;3,1;1,5;1,1;1,4;6,6;3,2;2,4;5,3;6,1;5,4;4,4;2,5;5,6;2,1;4,6;4,5;];
%temp_dst_routers = [1,6;4,4;2,1;6,1;3,4;6,4;6,2;3,5;1,1;6,3;2,4;3,3;4,6;2,3;4,1;5,3;1,3;5,1;5,2;3,6;];
%temp_dst_routers = [5,1;3,4;6,6;6,4;4,6;1,6;3,1;1,4;3,3;1,2;5,2;5,5;4,2;5,6;2,4;1,5;5,4;3,5;6,3;4,3;4,5;5,3;2,1;2,3;2,6;3,6;6,5;6,2;4,4;6,1;4,1;1,3;3,2;2,5;];

% A = (temp_dst_routers(:,1) - src_router(1));
% B = (temp_dst_routers(:,2) - src_router(2));
% src_dst_distances = sqrt(A.*A + B.*B);
%[ordered_distances distances_order] = sort(src_dst_distances,'ascend');
src_dst_distances = abs(temp_dst_routers(:,1) - src_router(1)) + abs(temp_dst_routers(:,2) - src_router(2));
[ordered_distances distances_order] = sort(src_dst_distances,'ascend');


%prepare figure
h=figure(1);
xlim([0 mesh_x_size + 1])
ylim([0 mesh_y_size + 1])
hold on;

mesh_routers = cell(mesh_x_size,mesh_y_size);
% create mesh of routers
for x_cnt = 1:mesh_x_size
    for y_cnt = 1:mesh_y_size
        flag = 0;
        mesh_routers{x_cnt,y_cnt} = router(x_cnt, y_cnt);
        for cnt=1:number_of_crash_routers
            % draw mesh routers
            if (((x_cnt == crash_routers(cnt,1)) && (y_cnt == crash_routers(cnt,2))))
                flag = 1;
                mesh_routers{x_cnt,y_cnt}.north_queue_occupancy = 10;
                mesh_routers{x_cnt,y_cnt}.south_queue_occupancy = 10;
                mesh_routers{x_cnt,y_cnt}.west_queue_occupancy = 10;
                mesh_routers{x_cnt,y_cnt}.east_queue_occupancy = 10;
            end
        end
        if (0 == flag)
            line([x_cnt, x_cnt],[y_cnt, y_cnt],'Marker','o','MarkerSize',12,'MarkerEdgeColor','k','MarkerFaceColor','w');
        end
    end
end

for dst_cnt=1:number_of_dst
    line([temp_dst_routers(distances_order(dst_cnt),1), temp_dst_routers(distances_order(dst_cnt),1)],[temp_dst_routers(distances_order(dst_cnt),2),temp_dst_routers(distances_order(dst_cnt),2)],'Marker','o','MarkerSize',12,'MarkerEdgeColor','k','MarkerFaceColor','y');
end

% draw src router
line([src_router(1), src_router(1)],[src_router(2), src_router(2)],'Marker','o','MarkerSize',12,'MarkerEdgeColor','k','MarkerFaceColor','g');
% meet routers themselves
for x_cnt = 1:mesh_x_size
    for y_cnt = 1:mesh_y_size
        if (x_cnt > 1 && x_cnt < mesh_x_size)
            if (y_cnt > 1 && y_cnt < mesh_y_size)
                mesh_routers{x_cnt,y_cnt}.north_router = mesh_routers{x_cnt, y_cnt + 1};
                mesh_routers{x_cnt,y_cnt}.south_router = mesh_routers{x_cnt, y_cnt - 1};
                mesh_routers{x_cnt,y_cnt}.west_router = mesh_routers{x_cnt - 1, y_cnt};
                mesh_routers{x_cnt,y_cnt}.east_router = mesh_routers{x_cnt + 1, y_cnt};
            elseif (y_cnt == 1)
                mesh_routers{x_cnt,y_cnt}.north_router = mesh_routers{x_cnt, y_cnt + 1};
                mesh_routers{x_cnt,y_cnt}.west_router = mesh_routers{x_cnt - 1, y_cnt};
                mesh_routers{x_cnt,y_cnt}.east_router = mesh_routers{x_cnt + 1, y_cnt};
            elseif (y_cnt == mesh_y_size)
                mesh_routers{x_cnt,y_cnt}.south_router = mesh_routers{x_cnt, y_cnt - 1};
                mesh_routers{x_cnt,y_cnt}.west_router = mesh_routers{x_cnt - 1, y_cnt};
                mesh_routers{x_cnt,y_cnt}.east_router = mesh_routers{x_cnt + 1, y_cnt};
            end
        elseif (x_cnt == 1)
            if (y_cnt > 1 && y_cnt < mesh_y_size)
                mesh_routers{x_cnt,y_cnt}.north_router = mesh_routers{x_cnt, y_cnt + 1};
                mesh_routers{x_cnt,y_cnt}.south_router = mesh_routers{x_cnt, y_cnt - 1};
                mesh_routers{x_cnt,y_cnt}.east_router = mesh_routers{x_cnt + 1, y_cnt};
            elseif (y_cnt == 1)
                mesh_routers{x_cnt,y_cnt}.north_router = mesh_routers{x_cnt, y_cnt + 1};
                mesh_routers{x_cnt,y_cnt}.east_router = mesh_routers{x_cnt + 1, y_cnt};
            elseif (y_cnt == mesh_y_size)
                mesh_routers{x_cnt,y_cnt}.south_router = mesh_routers{x_cnt, y_cnt - 1};
                mesh_routers{x_cnt,y_cnt}.east_router = mesh_routers{x_cnt + 1, y_cnt};
            end
        elseif (x_cnt == mesh_x_size)
            if (y_cnt > 1 && y_cnt < mesh_y_size)
                mesh_routers{x_cnt,y_cnt}.north_router = mesh_routers{x_cnt, y_cnt + 1};
                mesh_routers{x_cnt,y_cnt}.south_router = mesh_routers{x_cnt, y_cnt - 1};
                mesh_routers{x_cnt,y_cnt}.west_router = mesh_routers{x_cnt - 1, y_cnt};
            elseif (y_cnt == 1)
                mesh_routers{x_cnt,y_cnt}.north_router = mesh_routers{x_cnt, y_cnt + 1};
                mesh_routers{x_cnt,y_cnt}.west_router = mesh_routers{x_cnt - 1, y_cnt};
            elseif (y_cnt == mesh_y_size)
                mesh_routers{x_cnt,y_cnt}.south_router = mesh_routers{x_cnt, y_cnt - 1};
                mesh_routers{x_cnt,y_cnt}.west_router = mesh_routers{x_cnt - 1, y_cnt};
            end
        end
    end
end
%     % for dst_cnt=1:number_of_crash_channels
%     %     [a,b] = dsxy2figxy(gca,[crash_channels(dst_cnt,1),crash_channels(dst_cnt,3)],[crash_channels(dst_cnt,2),crash_channels(dst_cnt,4)]);
%     %     annotation('arrow',a,b,'LineStyle','--','Color','r','HeadStyle','vback3');
%     %     if (strcmp(crash_channels_dir(dst_cnt),'w'))
%     %         mesh_routers{crash_channels(dst_cnt,3),crash_channels(dst_cnt,4)}.west_queue_occupancy = 10;
%     %     elseif (strcmp(crash_channels_dir(dst_cnt),'e'))
%     %         mesh_routers{crash_channels(dst_cnt,3),crash_channels(dst_cnt,4)}.east_queue_occupancy = 10;
%     %     elseif (strcmp(crash_channels_dir(dst_cnt),'n'))
%     %         mesh_routers{crash_channels(dst_cnt,3),crash_channels(dst_cnt,4)}.north_queue_occupancy = 10;
%     %     elseif (strcmp(crash_channels_dir(dst_cnt),'s'))
%     %         mesh_routers{crash_channels(dst_cnt,3),crash_channels(dst_cnt,4)}.south_queue_occupancy = 10;
%     %     end
%     %
%     %
%     % end
%     % mesh_routers{3,2}.west_queue_occupancy = 10;
%     % mesh_routers{3,2}.east_queue_occupancy = 10;
%     % mesh_routers{3,2}.north_queue_occupancy = 10;
%     % mesh_routers{3,2}.south_queue_occupancy = 10;
%     %
%     % mesh_routers{2,3}       .south_queue_occupancy = 10;
% c ` % inject flit to mesh
for dst_cnt=1:number_of_dst
    mesh_routers{src_router(1),src_router(2)}.push_queue('inject',[temp_dst_routers(distances_order(dst_cnt),1),temp_dst_routers(distances_order(dst_cnt),2)]);
end
saveas(h,sprintf('zdj_%d.jpg',zdj_cnt));
%close(h);
end


